package 力扣._3164_优质数对的总数2;

import java.util.*;

class Solution {

    List<Integer> getYinZi(int n) {
        List<Integer> list = new LinkedList<>();
        for (int i = 1; i * i <= n; i++) {
            if (n % i == 0) {
                list.add(i);
                if (i != n / i)
                    list.add(n / i);
            }
        }
        System.out.println(list);
        return list;
    }

    public long numberOfPairs(int[] nums1, int[] nums2, int k) {
        // 枚举倍数
        long toatl = 0;

        Map<Integer, Integer> cnt1 = new HashMap<>();
        for (int x : nums1) {
            if (x % k == 0) {
                cnt1.merge(x / k, 1, Integer::sum);
            }
        }
        if(cnt1.isEmpty())
            return 0;
        Map<Integer, Integer> cnt2 = new HashMap<>();
        for (int x : nums2) {
            cnt2.merge(x, 1, Integer::sum);
        }

        int u = Collections.max(cnt1.keySet()); // v1 要被 v2整除，则v1要大于v2，这里枚举v2的倍数，要有一个上限

        Set<Integer> set2 = cnt2.keySet();
        for (Integer v2 : set2) {
            for (int t = v2; t <= u; t += v2) {
                if (cnt1.containsKey(t))
                    toatl += (long) cnt2.get(v2) * cnt1.get(t);
            }

        }

        return toatl;
    }
}